﻿(function() {
    var isIDevice = (/iphone|ipad/gi).test(navigator.appVersion);
    nova.widgets.BeepPlayer = function(src) {
        this.src = "";
        if (src) {
            this.src = $.trim(src);
        }
    };

    nova.widgets.BeepPlayer.prototype = {
        play: function(src) {
            var me = this;
            if (src) {
                me.src = $.trim(src);
            }
            if (me.src == "" || me.src == null) {
                navigator.notification.beep(2);
                return;
            }
            me._beep();
        },
        _beep: function() {
            var me = this;
            if (isIDevice || /Chrome\/\d/.test(navigator.userAgent)) {
                var $audio = $("#beepaudio");
                if (!$audio || $audio.length <= 0) {
                    $audio = $('<audio id="beepaudio" style="display: none"></audio>');
                    $("#content").append($audio);
                }
                var media = $audio[0];
                media.src = encodeURI(me.src);
                media.load();
                media.play();
            } else {
                var media = new Media(encodeURI(me.src), function() {
                    console.log("Audio Success.");
                }, function(err) {
                    console.log("Audio Error: " + err + ".");
                }, function(status) {
                    if (status == Media.MEDIA_STOPPED) {
                        media.release();
                    }
                });
                media.play();
            }
        }
    };
})();